TemplateParser: Don't fatal on cache misses
authorTimo Tijhof <krinklemail@gmail.com>
Sun, 22 Mar 2015 22:01:52 +0000 (22:01 +0000)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 23 Mar 2015 01:38:56 +0000 (01:38 +0000)
Also add regression test, and coverage for more methods.

Was trying to eval the code which had the hmac integrity check in front of it,
which causes a syntax error in valid PHP code.

Follows-up db1866da450c50bea2e.

Bug: T93436
Bug: T93511
Change-Id: Ie90074e4885de7340e53f59fdd479f5384b5eac6

includes/TemplateParser.php
tests/phpunit/data/templates/foobar.mustache [new file with mode: 0644]
tests/phpunit/includes/TemplateParserTest.php

index 65904a0..0131fe6 100644 (file)
@@ -74,7 +74,7 @@ class TemplateParser {
        /**
         * Returns a given template function if found, otherwise throws an exception.
         * @param string $templateName The name of the template (without file suffix)
-        * @return Function
+        * @return callable
         * @throws RuntimeException
         */
        public function getTemplate( $templateName ) {
@@ -114,11 +114,8 @@ class TemplateParser {
                        if ( !$code ) {
                                $code = $this->compileForEval( $fileContents, $filename );
 
-                               // Prefix the code with a keyed hash (64 hex chars) as an integrity check
-                               $code = hash_hmac( 'sha256', $code, $secretKey ) . $code;
-
-                               // Cache the compiled PHP code
-                               $cache->set( $key, $code );
+                               // Prefix the cached code with a keyed hash (64 hex chars) as an integrity check
+                               $cache->set( $key, hash_hmac( 'sha256', $code, $secretKey ) . $code );
                        } else {
                                // Verify the integrity of the cached PHP code
                                $keyedHash = substr( $code, 0, 64 );
diff --git a/tests/phpunit/data/templates/foobar.mustache b/tests/phpunit/data/templates/foobar.mustache
new file mode 100644 (file)
index 0000000..a042389
--- /dev/null
@@ -0,0 +1 @@
+hello world!
index ccfccd1..f884a8e 100644 (file)
@@ -4,6 +4,58 @@
  * @group Templates
  */
 class TemplateParserTest extends MediaWikiTestCase {
+
+       protected $templateDir;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->setMwGlobals( array(
+                       'wgSecretKey' => 'foo',
+                       'wgMemc' => new EmptyBagOStuff(),
+               ) );
+
+               $this->templateDir = dirname( __DIR__ ) . '/data/templates/';
+       }
+
+       /**
+        * @covers TemplateParser::getTemplateFilename
+        * @dataProvider provideGetTemplateFilename
+        */
+       public function testGetTemplateFilename( $dir, $name, $result, $exception = false ) {
+               if ( $exception ) {
+                       $this->setExpectedException( $exception );
+               }
+
+               $tp = new TemplateParser( $dir );
+               $path = $tp->getTemplateFilename( $name );
+               $this->assertEquals( $result, $path );
+       }
+
+       public static function provideGetTemplateFilename() {
+               return array(
+                       array(
+                               'dir/templates',
+                               'foobar',
+                               'dir/templates/foobar.mustache',
+                       ),
+                       array(
+                               'dir/templates',
+                               '../foobar',
+                               '',
+                               'UnexpectedValueException'
+                       ),
+               );
+       }
+
+       /**
+        * @covers TemplateParser::getTemplate
+        */
+       public function testGetTemplate() {
+               $tp = new TemplateParser( $this->templateDir );
+               $this->assertTrue( is_callable( $tp->getTemplate( 'foobar' ) ) );
+       }
+
        /**
         * @covers TemplateParser::compile
         */